﻿using System;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
namespace Spider.Forms
{
    public partial class SystemConfig : DockContent
    {
        public SystemConfig()
        {
            InitializeComponent();
        }
        string filepath = AppDomain.CurrentDomain.BaseDirectory.Replace("bin\\Debug\\", "") + "BaseConfig.xml";

        private void SystemConfig_Load(object sender, EventArgs e)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(filepath);
            txtWebSiteDir.Text = doc.SelectSingleNode("BaseConfigInfo/WebSiteDir").InnerText;
            //txtConnectionString.Text = doc.SelectSingleNode("BaseConfigInfo/ConnectionString").InnerText;
            txtCategory.Text = doc.SelectSingleNode("BaseConfigInfo/Category").InnerText;
            txtTag.Text = doc.SelectSingleNode("BaseConfigInfo/Tag").InnerText;
            txtFilter.Text = doc.SelectSingleNode("BaseConfigInfo/Filter").InnerText;
            txtWaterMarkJpg.Text = doc.SelectSingleNode("BaseConfigInfo/WaterMarkJpg").InnerText;
            txtImgHeigh.Text = doc.SelectSingleNode("BaseConfigInfo/ImgHeigh").InnerText;
            ckbEnableMark.Checked =bool.Parse( doc.SelectSingleNode("BaseConfigInfo/EnableMark").InnerText);
        }
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (FolderBrowserDialog dialog = new FolderBrowserDialog())
            {
                if (DialogResult.OK == dialog.ShowDialog())
                {
                    txtWebSiteDir.Text = dialog.SelectedPath;
                }
            }
        }

        //private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    txtConnectionString.Text = "data source=.;initial catalog=数据库名;User ID=用户名;Password=密码;";
        //}

        private void btnOK_Click(object sender, EventArgs e)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(filepath);
            doc.SelectSingleNode("BaseConfigInfo/WebSiteDir").InnerText = txtWebSiteDir.Text;
            //doc.SelectSingleNode("BaseConfigInfo/ConnectionString").InnerText = txtConnectionString.Text;
            doc.SelectSingleNode("BaseConfigInfo/Category").InnerText = txtCategory.Text;
            doc.SelectSingleNode("BaseConfigInfo/Tag").InnerText = txtTag.Text;
            doc.SelectSingleNode("BaseConfigInfo/Filter").InnerText = txtFilter.Text;
            doc.SelectSingleNode("BaseConfigInfo/WaterMarkJpg").InnerText = txtWaterMarkJpg.Text;
            doc.SelectSingleNode("BaseConfigInfo/ImgHeigh").InnerText = txtImgHeigh.Text;
            doc.SelectSingleNode("BaseConfigInfo/EnableMark").InnerText = ckbEnableMark.Checked.ToString();
            doc.Save(filepath);

            btnCancel_Click(null, null);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                if (dialog.ShowDialog(this) == DialogResult.OK)
                {
                    txtWaterMarkJpg.Text = dialog.FileName;
                }

            }
        }
    }
}
